package com.ymh.controller;

import com.ymh.common.domain.ResultData;
import com.ymh.domain.AccountDTO;
import com.ymh.fegin.AccountFeign;
import com.ymh.service.AccountService;
import java.math.BigDecimal;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: 13590
 * @date: 2020/10/27 18:25
 * @description:
 */
@RestController
@Log4j2
public class AccountController implements AccountFeign {
  @Autowired
  private AccountService accountService;

  @Override
  public ResultData<String> insert( @RequestBody AccountDTO accountDTO) {
    log.info("insert account:{}", accountDTO);
    accountService.insertAccount(accountDTO);
    return ResultData.success("insert account succeed");
  }

  @Override
  public ResultData<String> delete(@RequestParam("accountCode")  String accountCode) {
    log.info("delete account,accountCode is {}", accountCode);
    accountService.deleteAccount(accountCode);
    return ResultData.success("delete account succeed");
  }

  @Override
  public ResultData<String> update( @RequestBody AccountDTO accountDTO) {
    log.info("update account:{}", accountDTO);
    accountService.updateAccount(accountDTO);
    return ResultData.success("update account succeed");
  }

  @Override
  public ResultData<AccountDTO> getByAccountCode(@PathVariable(value = "accountCode") String accountCode) {
    log.info("get account detail,accountCode is :{}", accountCode);
    AccountDTO accountDTO = accountService.selectByCode(accountCode);
    return ResultData.success(accountDTO, "get account succeed");
  }

  @Override
  public ResultData<String> reduce(@RequestParam("accountCode") String accountCode, @RequestParam("amount") BigDecimal mount) {

    log.info("reduce mount of account,accountCode is :{}", accountCode);
    accountService.reduce(accountCode, mount);
    return ResultData.success("reduce mount of account succeed");
  }

}
